package com.park.web.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.moments.MomentsCommentModel;
import com.park.client.model.moments.MomentsModel;
import com.park.client.model.moments.MomentsThumbUpModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.moments.MomentsCommentService;
import com.park.client.service.moments.MomentsService;
import com.park.client.service.moments.MomentsThumbUpService;
import com.park.common.constant.Constant;
import com.park.common.util.Result;
import com.park.service.impl.websocket.MessageHandler;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/user/moments")
public class UserMomentsController extends BaseController{

	@Autowired
	private MomentsService momentsService;
	@Autowired
	private MomentsThumbUpService iMomentsThumbUpService;
	@Autowired
	private MomentsCommentService iMomentsCommentService;
	@Autowired
	private MessageHandler messageHandler;
	
	@RequestMapping("signListByUserId")
	@ResponseBody
	public List<MomentsModel> signListByUserId(Integer startPos,HttpServletRequest request,HttpSession session){
			/*User user = (User) session.getAttribute("user");
			List<Moments> moments = momentsService.list(user.getId());
			request.setAttribute("moments", moments);
			return Global.USER_PATH+"account/momentsList";*/
			
			UserModel user = (UserModel) session.getAttribute("user");
			Map<String,Object> map =new HashMap<>();
			map.put("userId", user.getId());
			map.put("startPos",startPos);
			List<MomentsModel>  moments = momentsService.selectByUserId(map);
			return moments;
	}
	
	@RequestMapping("momentsList")
	public String signList(HttpServletRequest request,HttpSession session){
		List<MomentsModel> moments = momentsService.list(SessionUtil.getUserID());
		request.setAttribute("moments", moments);
		return Global.USER_PATH+"account/momentsList";
	}
	
	/**
	 * 动态赞
	 */
	@RequestMapping("momentsThumbUp")
	@ResponseBody
	public Result<MomentsModel> momentsThumbUp(MomentsThumbUpModel thumbUp,HttpServletRequest request,HttpSession session){
		Result<MomentsModel> result = new Result<>(true);
		UserModel user = (UserModel) session.getAttribute("user");
		if(user != null){
			thumbUp.setCreater(user.getId());
			MomentsThumbUpModel m = iMomentsThumbUpService.selectIsExists(thumbUp);
			if(m == null){
					thumbUp.setCreateDate(new Date());
					boolean success =  iMomentsThumbUpService.insertSelective(thumbUp);
					if(success){//成功
						MomentsModel moments  = momentsService.selectByPrimaryKey(thumbUp.getMomentsId());
						moments.setThumbUpNum(moments.getThumbUpNum()+1);
						momentsService.updateByPrimaryKeySelective(moments);
						result.setMessage(Constant.SUCCESS_THUMB_UP_MSG);
						result.setCode(Constant.SUCCESS100);
						result.setObj(moments);
					}else{//失败
						result.setMessage(Constant.FAIL_THUMB_UP_MSG);
						result.setCode(Constant.FAIL101);
						result.setSuccess(false);
					}
				}else{//已经点赞过
					result.setMessage(Constant.HAS_THUMB_UP_MSG);
					result.setCode(Constant.FAIL103);
					result.setSuccess(false);
				}
		}else{//未登录
			result.setMessage(Constant.NOT_LOGGED_MSG);
			result.setCode(Constant.NOT_LOGGED102);
			result.setSuccess(false);
		}
		return result;
	}
	
	/**
	 * 发表动态
	 */
	@RequestMapping("publicMoment")
	@ResponseBody
	public Result<MomentsModel> publicMoment(MomentsModel moments,HttpServletRequest request,HttpSession session){
		Result<MomentsModel> result = new Result<>(true);
		moments.setCreater(SessionUtil.getUserID(session));
		moments.setCreateDate(new Date());
		boolean success =  momentsService.insertSelective(moments);
		if(success){//成功
			result.setMessage(Constant.SUCCESS_SAVE_MSG);
			result.setObj(moments);
		}else{//失败
			result.setMessage(Constant.FAIL_SAVE_MSG);
			result.setSuccess(Constant.FAIL);
		}
		return result;
	}
	
	/**
	 * 动态评论
	 */
	@RequestMapping("momentsComment")
	@ResponseBody
	public Result<MomentsModel> momentsComment(MomentsCommentModel momentsComment,HttpServletRequest request,HttpSession session){
		Result<MomentsModel> json = new Result<>(true);
		UserModel user = (UserModel) session.getAttribute("user");
		if(user != null){
			MomentsCommentModel m = iMomentsCommentService.selectIsExists(momentsComment);
			if(m == null){
				momentsComment.setCommentDate(new Date());
				boolean success =  iMomentsCommentService.insertSelective(momentsComment);
				if(success){//成功
					MomentsModel moments  = momentsService.selectByPrimaryKey(momentsComment.getMomentsId());
					moments.setCommentNum(moments.getCommentNum()+1);
					momentsService.updateByPrimaryKeySelective(moments);
					json.setMessage(Constant.SUCCESS_COMMENT_MSG);
					json.setCode(Constant.SUCCESS100);
					json.setObj(moments);
				}else{//失败
					json.setMessage(Constant.FAIL_COMMENT_MSG);
					json.setCode(Constant.FAIL101);
					json.setSuccess(false);
				}
			}else{//已经评论过
				json.setMessage(Constant.HAS_COMMENT_MSG);
				json.setCode(Constant.FAIL103);
				json.setSuccess(false);
			}
		}else{//未登录
			json.setMessage(Constant.NOT_LOGGED_MSG);
			json.setCode(Constant.NOT_LOGGED102);
			json.setSuccess(false);
		}
		return json;
	}
	
	
	/**
	 *单个动态查看 
	 */
	@RequestMapping("detail")
	public String detail(@RequestParam("id") Integer id,HttpServletRequest request){
//		Moments moment = momentsService.selectLatestMoment(id);
//		request.setAttribute("moment", moment);
		return Global.USER_PATH+"moments/momentsDetail";
	}
	
	
}
